package asapIs134CK.womi.client.games.sudoku;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.DomEvent;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class SudokuUnknownTile extends Composite {
	protected static final char DELETEKEY = 8;
	private FlowPanel panel;
	private TextBox input;
	private Label annotations;
	private List possibilities;
	private int answer;
	private int userChoice;
	private boolean editMode;
	
	public SudokuUnknownTile(int ans){
		panel = new FlowPanel();
		panel.sinkEvents(Event.ONCLICK|Event.ONDBLCLICK|Event.ONKEYPRESS|Event.ONKEYUP|Event.ONKEYDOWN);

		input = new TextBox();
		annotations = new Label("");
		annotations.addStyleName("Annotations");
		possibilities = new ArrayList(9);
		answer = ans;
		userChoice = 0;
		editMode = true;
		initSudokuTile();
		initWidget(panel);
	}
	
	private void initSudokuTile() {
		KeyPressHandler inputKeyPressHandler = new KeyPressHandler(){		
			//@Override
			public void onKeyPress(KeyPressEvent event) {
				if((input.getText().length()!=0 || !isValidKey(event.getCharCode()))&& event.getCharCode()!=DELETEKEY){
					event.preventDefault();
				}
			}
		};
		ValueChangeHandler inputValueChangehandler = new ValueChangeHandler(){
			public void onValueChange(ValueChangeEvent event){
				if(input.getText() == ""){
					userChoice = 0;
				}
				else {
					userChoice = Integer.parseInt(input.getText());
				}
			}
		};
		input.addValueChangeHandler(inputValueChangehandler);
		input.addKeyPressHandler(inputKeyPressHandler );
		panel.add(input);
		annotations.setVisible(false);
		panel.add(annotations);
	}

	public void toggleMode(){
		editMode = !editMode;
		if(editMode){
			annotations.setVisible(false);
			input.setVisible(true);
			input.setFocus(true);
		}
		else {
			input.setVisible(false);
			annotations.setVisible(true);
		}
	}
	
	private String getPossibilities() {
		String res = "";
		Iterator ite = possibilities.iterator();
		while(ite.hasNext()){
			Object i = ite.next();
			if(i instanceof Character){
				res += i+" ";
			}
		}
		return res;
	}
	public void onBrowserEvent(Event event) {
		switch (DOM.eventGetType(event)) { 
	        case Event.ONCLICK: {
	        	if(editMode){
	        		input.setFocus(true);
	        	}
	        	break; 
	        }
	        case Event.ONDBLCLICK: {
	        	toggleMode();
	        	break; 
	        }
	        case Event.ONKEYPRESS: {
	        	if(!editMode && isValidKey(event.getKeyCode())){
	        		handleAnnotationsKeyPress(event.getKeyCode());
	        	}
	        	break;
	        }
	        default: { 
	            // Do nothing 
	        } 
    } 
	}

	private void handleAnnotationsKeyPress(int keyCode) {
		Character car = new Character((char)keyCode);
		if(possibilities.contains(car)){
			possibilities.remove(car);
		}
		else{
			possibilities.add(car);
			Collections.sort(possibilities);
		}
		displayPossibilities();
	}

	private void displayPossibilities() {
		annotations.setText(getPossibilities());
	}

	private boolean isValidKey(int keyCode) {
		boolean res = false;
		if(Character.isDigit((char)keyCode)&& keyCode != '0'){
			res = true;
		}
		return res;
	}

	public boolean isCorrect() {
		// TODO Auto-generated method stub
		return userChoice == answer;
	}

	public void displayHint() {
		userChoice = answer;
		input.setText(userChoice+"");
		if(!editMode){
			toggleMode();
		}
	}
}
